import winston from 'winston';
import config from '@/config';

const logger = winston.createLogger({
  level: config.logging.level,
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  defaultMeta: { service: 'chainwatch' },
  transports: [
    new winston.transports.File({ 
      filename: config.logging.file.replace('.log', '-error.log'),
      level: 'error'
    }),
    new winston.transports.File({ 
      filename: config.logging.file
    })
  ]
});

// 开发环境下同时输出到控制台
if (config.app.env !== 'production') {
  logger.add(new winston.transports.Console({
    level: 'info', // 显式设置控制台日志级别为info
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.simple()
    )
  }));
}

export default logger;

// 创建特定模块的日志器
export const createModuleLogger = (module: string) => {
  return logger.child({ module });
};

// 常用日志方法
export const logTransaction = (tx: any, chain: string) => {
  logger.info('Large transaction detected', {
    module: 'monitor',
    chain,
    hash: tx.hash,
    from: tx.from,
    to: tx.to,
    value: tx.value,
    valueUSD: tx.valueUSD
  });
};

export const logError = (error: Error, context?: any) => {
  logger.error(error.message, {
    error: error.stack,
    context
  });
};

export const logAlert = (alertId: string, channels: string[], success: boolean) => {
  logger.info('Alert sent', {
    module: 'alert',
    alertId,
    channels,
    success
  });
}; 